<?php  
  
namespace App\Http\Middleware;  
  
use Closure;  
use Illuminate\Support\Facades\Auth;  
  
//   用户中间件
class UserAuthMiddleware  
{  
    /** 
     * Handle an incoming request. 
     * 
     * @param  \Illuminate\Http\Request  $request 
     * @param  \Closure  $next 
     * @return mixed 
     */  
    public function handle($request, Closure $next, $guard = null)  
    {  
        // Auth类（内置）验证当前用户是否是登录状态
        if (Auth::guard($guard)->guest()) {  
            if ($request->ajax() || $request->wantsJson()) {  
                return response('Unauthorized.', 401);  
            } else {
                //   不存在则返回登录页
                return redirect()->guest('admin/login');  
            }  
        }  
        // 已登录的话，继续请求实际应该请求的url
        return $next($request);  
    }  
}  